<template>
    <Teleport to="body" :disabled="!visible">
        <div
            v-if="visible"
            class="lightbox-overlay"
            @click.self="handleClose"
            @wheel="handleWheel"
            @keydown.esc="handleClose"
            @keydown.left="handlePrev"
            @keydown.right="handleNext"
            tabindex="0"
            ref="lightboxRef"
        >
            <div class="lightbox-header">
                <span>{{ currentIndex + 1 }} / {{ imageList.length }}</span>
                <CloseOutlined class="close-icon" @click="handleClose" />
            </div>
            <div class="lightbox-body">
                <a-button
                    type="text"
                    size="large"
                    class="nav-btn nav-btn-prev"
                    @click="handlePrev"
                    :disabled="imageList.length <= 1"
                >
                    <template #icon>
                        <LeftOutlined />
                    </template>
                </a-button>

                <div class="image-container">
                    <img
                        :src="imageList[currentIndex]"
                        :alt="`Image ${currentIndex + 1}`"
                        class="preview-image"
                        :style="{
                            transform: `scale(${scale}) translate(${translateX}px, ${translateY}px)`,
                            cursor: isDragging ? 'grabbing' : 'grab'
                        }"
                        @mousedown="startDrag"
                        @mousemove="onDrag"
                        @mouseup="stopDrag"
                        @mouseleave="stopDrag"
                    />
                </div>

                <a-button
                    type="text"
                    size="large"
                    class="nav-btn nav-btn-next"
                    @click="handleNext"
                    :disabled="imageList.length <= 1"
                >
                    <template #icon>
                        <RightOutlined />
                    </template>
                </a-button>
            </div>
            <div class="lightbox-footer">
                <a-button size="small" @click="scale = Math.max(0.5, scale - 0.25)">-</a-button>
                <span class="zoom-level">{{ Math.round(scale * 100) }}%</span>
                <a-button size="small" @click="scale = Math.min(3, scale + 0.25)">+</a-button>
                <a-button size="small" @click="resetZoom">重置</a-button>
            </div>
        </div>
    </Teleport>
</template>

<script setup lang="ts">
import { CloseOutlined, LeftOutlined, RightOutlined } from "@ant-design/icons-vue";
import { nextTick, ref, watch } from "vue";

// 定义图片列表类型
type ImageList = string[];

// 定义 Props 类型
interface LightboxProps {
    imageList: ImageList;
    visible: boolean;
    initialIndex?: number;
}

// 定义 Emits 类型
type LightboxEmits = {
    "update:visible": [value: boolean];
    close: [];
};

// 使用 withDefaults 定义默认值
const props = withDefaults(defineProps<LightboxProps>(), {
    initialIndex: 0
});

const emit = defineEmits<LightboxEmits>();

const currentIndex = ref<number>(0);
const scale = ref<number>(1);
const isDragging = ref<boolean>(false);
const lastX = ref<number>(0);
const lastY = ref<number>(0);
const translateX = ref<number>(0);
const translateY = ref<number>(0);

const lightboxRef = ref<HTMLDivElement | null>(null);

// 监听 visible 变化，重置状态
watch(
    () => props.visible,
    (newVal: boolean) => {
        if (newVal) {
            currentIndex.value = props.initialIndex;
            resetZoom(); // 重置缩放和平移
            // 等待 DOM 更新后聚焦
            nextTick(() => {
                if (lightboxRef.value) {
                    lightboxRef.value.focus();
                }
            });
        }
    }
);

const handleClose = (): void => {
    emit("update:visible", false);
    emit("close");
};

const handlePrev = (): void => {
    if (props.imageList.length <= 1) return;
    currentIndex.value = (currentIndex.value - 1 + props.imageList.length) % props.imageList.length;
    resetZoom(); // 切换图片时重置缩放和平移
};

const handleNext = (): void => {
    if (props.imageList.length <= 1) return;
    currentIndex.value = (currentIndex.value + 1) % props.imageList.length;
    resetZoom(); // 切换图片时重置缩放和平移
};

const handleWheel = (e: WheelEvent): void => {
    e.preventDefault();
    if (e.deltaY < 0) {
        // 向上滚动放大
        scale.value = Math.min(3, scale.value + 0.1);
    } else {
        // 向下滚动缩小
        scale.value = Math.max(0.5, scale.value - 0.1);
    }
};

const startDrag = (e: MouseEvent): void => {
    if (scale.value <= 1) return; // 只有在放大时才允许拖拽
    isDragging.value = true;
    lastX.value = e.clientX - translateX.value;
    lastY.value = e.clientY - translateY.value;
    document.body.style.userSelect = "none";
};

const onDrag = (e: MouseEvent): void => {
    if (!isDragging.value) return;
    translateX.value = e.clientX - lastX.value;
    translateY.value = e.clientY - lastY.value;
};

const stopDrag = (): void => {
    isDragging.value = false;
    document.body.style.userSelect = "";
};

const resetZoom = (): void => {
    scale.value = 1;
    translateX.value = 0;
    translateY.value = 0;
};
</script>

<style scoped>
.lightbox-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.9);
    display: flex;
    flex-direction: column;
    z-index: 1050;
    outline: none;
}

.lightbox-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    color: white;
    background-color: rgba(0, 0, 0, 0.5);
}

.close-icon {
    font-size: 24px;
    cursor: pointer;
    transition: color 0.3s;
}

.close-icon:hover {
    color: #1890ff;
}

.lightbox-body {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

.nav-btn {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    color: white;
    background: rgba(0, 0, 0, 0.5);
    border: none;
    border-radius: 50%;
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1;
}

.nav-btn:hover {
    background: rgba(0, 0, 0, 0.7);
}

.nav-btn:disabled {
    color: rgba(255, 255, 255, 0.3);
    cursor: not-allowed;
    background: rgba(0, 0, 0, 0.3);
}

.nav-btn-prev {
    left: 20px;
}

.nav-btn-next {
    right: 20px;
}

.image-container {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
}

.preview-image {
    max-width: 90vw;
    max-height: 80vh;
    object-fit: contain;
    transition: transform 0.1s ease;
}

.lightbox-footer {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 16px;
    gap: 12px;
    color: white;
    background-color: rgba(0, 0, 0, 0.5);
}

.zoom-level {
    min-width: 50px;
    text-align: center;
}
</style>
